<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-table
      ref="multipleTable"
      :data="dataList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-loading="dataListLoading"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="有效日期" width="120">
        <template slot-scope="scope">{{ scope.row.endTime }}</template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="120"></el-table-column>
      <el-table-column prop="conditions" label="优惠条件" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.conditions == -1 ? "无限制": ("满"+scope.row.conditions+ "可使用") }}
        </template>
      </el-table-column>
      <el-table-column v-if="type === 2" prop="cost" label="金额" show-overflow-tooltip></el-table-column>
      <el-table-column v-if="type === 1" prop="roomNames" label="房型" show-overflow-tooltip></el-table-column>
      <el-table-column label="数量" >
        <template slot-scope="scope">
          <el-input-number class="el-input" v-model="scope.row.sendNum" :min="0" :max="scope.row.maxNum" label="天"></el-input-number>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button
        type="primary"
        @click="submitHandler()"
        :disabled="multipleSelection.length <= 0"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      type: 0,
      title: "",
      dialogVisible: false,
      dataList: [],
      multipleSelection: [],
      dataListLoading: false,
      memberDataList: []
    };
  },
  methods: {
    submitHandler() {
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelcoupons/sendCoupons"),
        method: "POST",
        data: this.$http.adornData({
          memberIds: this.memberDataList.map(item => {
            return item.userId;
          }),
          couponsIds: this.multipleSelection.map(item => {
            return item.id;
          }),
          type: this.type,
          sendNums: this.multipleSelection.map(item => {
            return item.sendNum;
          })
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.close();
              this.$emit("refreshDataList");
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    close() {
      this.dataList = [];
      this.dialogVisible = false;
    },
    init(type, memberDataList) {
      this.memberDataList = memberDataList;
      this.type = type;
      if (type === 1) {
        this.title = "请选择免房券";
        this.getCounpons();
      }
      if (type === 2) {
        this.title = "请选择优惠券";
        this.getCashCounpons();
      }
      if (type === 3) {
        this.title = "请选择早餐券";
        this.getBreakfastCounpons();
      }
      this.dialogVisible = true;
    },
    //免房券
    getCounpons() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelcoupons/listAll"),
        method: "get"
      }).then(({ data }) => {
        this.dataListLoading = false;
        if (data && data.code === 0) {
          this.dataList = data.data.map((item) => {
            const subNum = item.number - item.lqNum;
            item.maxNum = subNum < item.klqzs ? subNum : item.klqzs;
            item.sendNum = item.klqzs;
            item.roomNames = "";
            return item;
          });
          this.updateRoomList();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    updateRoomList() {
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelroom/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 20
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          const roomList = data.page.list;
          for (let i = 0; i < this.dataList.length; i++) {
            let coupon = this.dataList[i];
            if (coupon.roomIds != null) {
              coupon.roomNames = roomList
                .filter(i => coupon.roomIds.indexOf(i.id) > -1).map(i => i.name + ',');
            }
          }
        }
      });
    },

    //优惠券
    getCashCounpons() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelcouponscash/listAll"),
        method: "get"
      }).then(({ data }) => {
        this.dataListLoading = false;
        if (data && data.code === 0) {
          this.dataList = data.data.map((item) => {
            const subNum = item.number - item.lqNum;
            item.maxNum = subNum < item.klqzs ? subNum : item.klqzs;
            item.sendNum = item.klqzs;
            return item;
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //早餐券
    getBreakfastCounpons() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelcouponsbreakfast/listAll"),
        method: "get"
      }).then(({ data }) => {
        this.dataListLoading = false;
        if (data && data.code === 0) {
          this.dataList = data.data.map((item) => {
            const subNum = item.number - item.lqNum;
            item.maxNum = subNum < item.klqzs ? subNum : item.klqzs;
            item.sendNum = item.klqzs;
            return item;
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
